//
//  HMVisitorView.swift
//  weibo28
//
//  Created by 范英强 on 16/3/23.
//  Copyright © 2016年 范英强. All rights reserved.
//

import UIKit
import SnapKit
class HMVisitorView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 供外界调用的方法
    func setVisitorInfo(imgName: String?, title: String?){
        if let img = imgName, let tit = title{
            circleImageView.hidden = true
            iconImageView.image = UIImage(named: img)
            messageLabel.text = tit
            
        }else{
            setCircleAnim()
        }
    }
    
    // 动画
    func setCircleAnim(){
        // 定义anim
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI
        anim.duration = 10
        anim.repeatCount = MAXFLOAT
        // 当动画结束 或 界面处于不活跃状态 removedOnCompletion 默认 true
        anim.removedOnCompletion = false
        
        circleImageView.layer.addAnimation(anim, forKey: nil)
    }
    
    // MARK: - 设置视图
    
    private func setupUI(){
        backgroundColor = UIColor(white: 237 / 255, alpha: 1)
        // 1. 添加控件
        addSubview(circleImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        // 2. 添加约束
        
        // 使用SnapKit 约束
        // 01
        circleImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self)
        }
        // 02
        maskImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self)
        }
        // 03
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self)
        }
        // 04
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(maskImageView.snp_bottom).offset(16)
            make.centerX.equalTo(self)
            make.width.equalTo(230)
            make.height.equalTo(36)
        }
        // 05
        registerButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.leading.equalTo(messageLabel)
            make.size.equalTo(CGSize(width: 100, height: 35))
        }
        // 06
        loginButton.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(messageLabel)
            make.top.equalTo(registerButton)
            make.size.equalTo(registerButton)
        }

        // 取消aotoresizing
//        circleImageView.translatesAutoresizingMaskIntoConstraints = false
        // 系统约束
        /**
        *
        *
        *  @param circleImageView           约束的控件
        *  @param NSLayoutAttribute.CenterX 约束属性
        *  @param NSLayoutRelation.Equal    约束条件
        *  @param self                      相对于数来约束
        *  @param NSLayoutAttribute.CenterX 约束属性
        *  @param 1                         倍数
        *  @param 0                         浮点数
        *
        *  @return
        */
//        addConstraint(NSLayoutConstraint(item: circleImageView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//        
//        addConstraint(NSLayoutConstraint(item: circleImageView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
    }
    
    // MARK: - 懒加载控件
    // 圆圈
    private lazy var circleImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    // 遮挡
    private lazy var maskImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    // 房子
    private lazy var iconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    // 文字
    private lazy var messageLabel:UILabel = {
        
        let lab = UILabel(textColor: UIColor.darkGrayColor(), font: 14)
        // 对齐方式
        lab.textAlignment = .Center
        // 设置文字
        lab.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        // 换行
        lab.numberOfLines = 0
        return lab
    }()
    // 注册按钮
    lazy var registerButton: UIButton = {
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setTitleColor(themeColor, forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        return button
    }()
    // 登陆按钮
    lazy var loginButton: UIButton = {
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("登陆", forState: UIControlState.Normal)
        button.setTitleColor(themeColor, forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        return button
    }()

}
